package com.zhuhjay.eduservice.controller;


import com.zhuhjay.commonutils.R;
import com.zhuhjay.commonutils.ordervo.CourseOrder;
import com.zhuhjay.eduservice.entity.EduCourse;
import com.zhuhjay.eduservice.entity.frontvo.CourseWebVo;
import com.zhuhjay.eduservice.entity.vo.CourseInfoVo;
import com.zhuhjay.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-09-06
 */
@Api(description = "课程控制器")
@RestController
@RequestMapping("/eduservice/course")

public class EduCourseController {

    @Resource
    private EduCourseService courseService;

    @ApiOperation(value = "获取所有课程信息")
    @GetMapping
    public R findAll() {
        return R.ok().data("list", courseService.list(null));
    }

    @ApiOperation(value = "添加课程信息，包括课程描述")
    @PostMapping("addCourseInfo")
    @CacheEvict(value = "COURSE", allEntries = true)
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        return R.ok().data("courseId", courseService.saveCourseInfo(courseInfoVo));
    }

    @ApiOperation(value = "根据课程ID获取课程信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        return R.ok().data("courseInfoVo", courseService.getCourseInfo(courseId));
    }

    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourseInfo")
    @CacheEvict(value = "COURSE", allEntries = true)
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "获取发布的课程信息")
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId) {
        return R.ok().data("publishCourse", courseService.getPublishCourseInfo(courseId));
    }

    @ApiOperation(value = "修改课程状态为发布")
    @PostMapping("publishCourse/{id}")
    @CacheEvict(value = "COURSE", allEntries = true)
    public R publishCourse(@PathVariable String id) {
        courseService.updateById(new EduCourse().setId(id).setStatus("Normal"));
        return R.ok();
    }

    @ApiOperation(value = "删除课程信息")
    @DeleteMapping("{id}")
    @CacheEvict(value = "COURSE", allEntries = true)
    public R deleteCourse(@PathVariable String id) {
        courseService.deleteCourseById(id);
        return R.ok();
    }

    @ApiOperation(value = "order微服务获取课程信息")
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseOrder getCourseInfoOrder(@PathVariable String id) {
        CourseWebVo courseInfo = courseService.getFrontCourseInfo(id);
        CourseOrder courseOrder = new CourseOrder();
        BeanUtils.copyProperties(courseInfo, courseOrder);
        return courseOrder;
    }

}

